package com.lagou.service.impl;

import com.lagou.service.MailService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

import javax.annotation.Resource;

@Service
public class MailServiceImpl implements MailService {

	@Resource
	private JavaMailSender mailSender;

	@Value("${spring.mail.username}")
	private String from;

	@Override
	public void send(String email, String code) {
		SimpleMailMessage mailMessage = new SimpleMailMessage();
		mailMessage.setFrom(from);//发送者
		mailMessage.setTo(email);//接收者
		mailMessage.setSubject("拉勾");//标题
		mailMessage.setText("你的验证码为：<b>" + code + "</b><br/>请尽快完成注册！");//内容
		//异步发送
		Runnable runnable = () -> {
			try {
				mailSender.send(mailMessage);
			} catch (Exception e) {
				e.printStackTrace();
			}
		};
		runnable.run();
	}
}
